//
//  ByCellAddress.m
//  oc_knowledge
//
//  Created by mc on 17/1/12.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByCellAddress.h"
@interface ByCellAddress()
@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UILabel *userNameLabel;
@property (nonatomic, strong) UILabel *detailLabel;
@end
@implementation ByCellAddress

+ (instancetype)cellWithTableView:(UITableView*)tableView{
    
    static NSString *cellIdentifier = @"ByAddressCell";
    ByCellAddress *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setViews];
        
    }
    return self;
}

#pragma mark ---创建视图
- (void)setViews{
    //手机号
    _phoneLabel = [[UILabel alloc] init];
    _phoneLabel.textColor = [UIColor blackColor];
    _phoneLabel.font = FONT(16);
    _phoneLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:_phoneLabel];
    
    //名字
    _userNameLabel = [[UILabel alloc] init];
    _userNameLabel.textColor = [UIColor blackColor];
    _userNameLabel.font = FONT(16);
    [self addSubview:_userNameLabel];
    
    //详细地址
    _detailLabel = [[UILabel alloc] init];
    _detailLabel.textColor = [UIColor blackColor];
    _detailLabel.font = FONT(14);
    [self addSubview:_detailLabel];
    
}

-(void)setModel:(ByModelAddress *)model{
    _model = model;
    
    //手机号
    CGFloat x,y,width,height;
    width = [model.mobile getSizeOfString:16].width + 20;
    height = 20;
    y = 15;
    _phoneLabel.frame = CGRectMake(x, y, width, height);
    _phoneLabel.right = -30;
    _phoneLabel.text = model.mobile;
    
    //名字
    x = 10;
    width = SCREEN_WIDTH - 30 - width - 20;
    _userNameLabel.frame = CGRectMake(x, y, width, height);
    if ([_model.is_default isEqualToString:@"1"]) {
        NSString *detailStr = [NSString stringWithFormat:@" 默认  %@",model.contactname];
        NSMutableAttributedString *detailInfo = [[NSMutableAttributedString alloc] initWithString:detailStr];
        [detailInfo addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, 4)];
        [detailInfo addAttribute:NSBackgroundColorAttributeName value:BY_THEME_COLOR range:NSMakeRange(0, 4)];
        _userNameLabel.attributedText = detailInfo;
    }else{
        _userNameLabel.text = model.contactname;
        
    }
    
    //详细地址
    y = _userNameLabel.bottom + 10;
    width = SCREEN_WIDTH - x - 30;
    _detailLabel.frame = CGRectMake(x, y, width, height);
    if ([_model.city isEqualToString:@"市辖区"] || [_model.area isEqualToString:@"县"]) {
        _detailLabel.text = [NSString stringWithFormat:@"%@%@%@",_model.province,model.area,model.detailinfo];

    }else
        _detailLabel.text = [NSString stringWithFormat:@"%@%@%@%@",_model.province,model.city,model.area,model.detailinfo];


    
}
@end
